package pacBiopackage;

import java.io.BufferedReader;
import java.io.FileReader;


import tools.kmer.KmerSet_binary;

public class KmerToObjectFile {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		GetOpt go = new GetOpt(args, "k:o:h");
		go.optErr = true;
		int ch = -1;
		
		// process options in command line arguments
		// Set default values
		boolean usagePrint = false;                 
		String filenameOutput="output.txt";
		String filenameKmers ="";
		while ((ch = go.getopt()) != GetOpt.optEOF) 
		{
		    if ((char)ch == 'h') {
		       usagePrint = true;
		   }
		   else if((char)ch =='o')
		   {
			  filenameOutput = go.optArgGet();
		   }
		   else if((char)ch =='k')
		   {
			   filenameKmers=go.optArgGet();
		   }
		   else
		       System.exit(1);                     // undefined option
		}                                           // getopt() returns '?'
		
		if (usagePrint || filenameKmers.isEmpty()) {
		    System.out.println("Usage: -k Kmers [ -o outputFile -h]"); //NOI18N
		    System.exit(0);
		}
		
		// @TODO check if files exist
	
		System.out.println("------------------------");
		System.out.println("Command line parameter settings:");
		System.out.println("-k " + filenameKmers); //NOI18N
		System.out.println("-o " + filenameOutput); //NOI18N
		System.out.println("------------------------");
		
		
		
		KmerSet_binary ks= null;// new KmerSet_binary(kmersSize);
		BufferedReader in;
		try 
		{
			in = new BufferedReader(new FileReader(filenameKmers));
			String kmer;
			System.out.println("reading kmers");
			while(in.ready())
			{
				kmer=in.readLine();
				if(ks !=null)
				{
					 ks.addKmer(kmer.split("\t")[1],Integer.valueOf(kmer.split("\t")[0])); //already checked for revComp....
				}
				else
				{
					ks= new KmerSet_binary(kmer.split("\t")[1].length());
					ks.addKmer(kmer.split("\t")[1],Integer.valueOf(kmer.split("\t")[0]));
				}
			}
			System.out.println("done");
			System.out.println("serializing KmerHash");
			ObjectSerializing<KmerSet_binary> objectKmer = new ObjectSerializing<KmerSet_binary>();
			objectKmer.serializeObject(ks, filenameOutput);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
	}

}
